package com.sunjob.mroimg.config;

import com.sunjob.mroimg.interceptor.ImgInterceptor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Wangzurong
 * @create 2022-08-09 8:42
 */
@Configuration
@Slf4j
public class ImgConfig implements WebMvcConfigurer {
    @Autowired
    ImgConfiguration imgConfiguration;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 映射本地目录
        registry.addResourceHandler("/img/**").addResourceLocations("file:////"+imgConfiguration.getLocation());
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ImgInterceptor());
    }

    @PostConstruct
    public void init(){
        for(String dir:imgConfiguration.getDirs()){
            String tempDir = imgConfiguration.getLocation()+dir;
            File file= new File(tempDir);
            if(!file.exists()){
                // 创建文件夹
                log.info("创建文件夹:{}",file.getAbsolutePath());
                file.mkdirs();
            }
        }
    }

}
